{% from 'bootstrap5/utils.html' import render_icon %}

{% macro photo_card(photo) %}
<div class="photo-card card">
  <a class="card-thumbnail" href="{{ url_for('main.show_photo', photo_id=photo.id) }}">
    <img class="card-img-top portrait" src="{{ url_for('main.get_image', filename=photo.filename_s) }}">
  </a>
  <div class="card-body">
    {{ render_icon('suit-heart-fill') }} {{ photo.collectors_count }}
    {{ render_icon('chat-left-fill') }} {{ photo.comments_count }}
  </div>
</div>
{% endmacro %}

{% macro user_card(user) %}
<div class="user-card text-center">
  <a href="{{ url_for('user.index', username=user.username) }}">
    <img class="rounded avatar-m" src="{{ url_for('main.get_avatar', filename=user.avatar_m) }}">
  </a>
  <h6>
    <a class="text-decoration-none" href="{{ url_for('user.index', username=user.username) }}">{{ user.name }}</a>
  </h6>
  {{ follow_area(user) }}
</div>
{% endmacro %}

{% macro follow_area(user) %}
{% if current_user.is_authenticated %}
  {% if user != current_user %}
    {% if current_user.is_following(user) %}
    <form class="inline" method="post"
      action="{{ url_for('user.unfollow', username=user.username, next=request.full_path) }}">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
      <button type="submit" class="btn btn-secondary btn-sm">Unfollow</button>
      {% if current_user.is_followed_by(user) %}
      <p class="badge text-bg-light rounded-pill">Follow each other</p>
      {% endif %}
    </form>
    {% else %}
    <form class="inline" method="post"
      action="{{ url_for('user.follow', username=user.username, next=request.full_path) }}">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
      <button type="submit" class="btn btn-primary btn-sm">Follow</button>
      {% if current_user.is_followed_by(user) %}
      <p class="badge text-bg-light rounded-pill">Follows you</p>
      {% endif %}
    </form>
    {% endif %}
  {% endif %}
{% else %}
<form class="inline" method="post" action="{{ url_for('user.follow', username=user.username) }}">
  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  <button type="submit" class="btn btn-primary btn-sm">Follow</button>
</form>
{% endif %}
{% endmacro %}
